package communication;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Communicator {

	// private Reader reader = new Reader();
	private BTCommandWrapper btCommandWrapper;

	public Communicator() {

		BTConnection connection = new BTConnection();
		
		btCommandWrapper = new BTCommandWrapper(connection);
		
		
		Thread bt_thread = new Thread(connection);
		bt_thread.start();
		try {
			bt_thread.join();
		} catch (InterruptedException e) {
			System.out.println("BTException");
		}

	}


	public void travel(int distance) {
		System.out.println("start travel");

		btCommandWrapper.SendCommandAndReceive(Command.TRAVEL, distance);

		
		System.out.println("stop travel");
	}

	

	public void terminate() {
		btCommandWrapper.SendCommandAndReceive(Command.TERMINATE, 0);

		
	}

	// positief == links!!
	public void rotateRobot(int angle) {
		btCommandWrapper.SendCommandAndReceive(Command.ROTATE, angle);

		
	}

	public void rotateSensorMotor(int angle) {
		btCommandWrapper.SendCommandAndReceive(Command.ROTATESENSOR, angle);

	}

	public int getDistance() {
		return btCommandWrapper.SendCommandAndReceive(Command.GETDISTANCE, 0);		
	}

	public int getDirectionIR() {
		return btCommandWrapper.SendCommandAndReceive(Command.GETIR, 0);

	}

	public int getLightValue() {
		return btCommandWrapper.SendCommandAndReceive(Command.GETLIGHT, 0);

	}

	public String getBarcode() {

		String output = "";
		int reading = btCommandWrapper.receiveBarcodeData();

		// -1 = end of barcode
		while (reading != -1) {
			output += Character.toString((char)reading);
			reading = btCommandWrapper.receiveBarcodeData();
		}

		System.out.println(output);
		return output;
	}

	
}
